package com.sddr.base.frame.metadata;

import com.sddr.base.frame.memory.ConfigMemory;
import com.sddr.common.constant.LinkFlowConstant;
import com.sddr.common.constant.LinkFlowMetaDataConstant;
import com.sddr.common.entity.ConfigRule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author 山顶洞人郑某
 * @className AbstractMetaDataOperation.java 元数据抽象模板
 * @date 2025 06 04
 */
public abstract class AbstractMetaDataOperation implements MetaDataOperation{

    private static final String GROUP_KEY = "group";


    /**
     * 获取注册中心
     */
    protected final Registration registration;


    /**
     * 配置信息存储
     */
    protected final ConfigMemory configMemory;

    public AbstractMetaDataOperation(Registration registration, ConfigMemory configMemory) {
        this.registration = registration;
        this.configMemory = configMemory;
    }

    /**
     * 获取元数据
     * @return 服务元数据
     */
    protected Map<String,String> getMetadata() {
        return registration.getMetadata();
    }

    /**
     * 获取分组
     * @param groupKey 分组key
     * @return 分组
     */
    protected String getGroup(String groupKey) {
        return getMetadata().get(groupKey);
    }

    /**
     * 从服务实例获取分组key
     * @param server 服务
     * @return 分组key
     */
    public String getGroupKeyFromServer(ServiceInstance server) {
        String groupKey = getMetadataFromServer(server).get(LinkFlowMetaDataConstant.SPRING_APPLICATION_GROUP_KEY);
        if (StringUtils.isEmpty(groupKey)) {
            groupKey = LinkFlowConstant.GROUP;
        }
        return groupKey;
    }

    @Override
    public String getVersionFromServer(ServiceInstance server) {
        String version = getMetadataFromServer(server).get(LinkFlowConstant.VERSION);
        return Optional.ofNullable(version).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }

    /**
     * 获取元数据结果
     * @param server 服务
     * @return 结果
     */
    public abstract Map<String ,String > getMetadataFromServer(ServiceInstance server);

    /**
     * 获取配置的规则
     * @return
     */
    @Override
    public ConfigRule getConfigRule() {
        return configMemory.getConfig(LinkFlowConstant.RULE);
    }

    /**
     * 获取服务ID
     * @param server 服务
     * @return
     */
    @Override
    public String getServiceIdFromServer(ServiceInstance server) {
        String serviceId = getMetadataFromServer(server).get(LinkFlowMetaDataConstant.SPRING_APPLICATION_NAME);
        if (StringUtils.isEmpty(serviceId)) {
            serviceId = server.getServiceId();
        }
        return Optional.ofNullable(serviceId).filter(StringUtils::isNotEmpty).orElseThrow(() ->
                new RuntimeException("serviceId from server is null"));
    }

    @Override
    public String getVersion(){
        String version = getMetadata().get(LinkFlowConstant.VERSION);
        return Optional.ofNullable(version).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }
    @Override
    public String getZone() {
        String zone = getMetadata().get(LinkFlowConstant.ZONE);
        return Optional.ofNullable(zone).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }

    @Override
    public String getGray() {
        String gray = getMetadata().get(LinkFlowConstant.GRAY);
        return Optional.ofNullable(gray).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.FALSE_STR);
    }

    @Override
    public String getZoneFromServer(ServiceInstance server) {
        String serverZone = getMetadataFromServer(server).get(LinkFlowConstant.ZONE);
        return Optional.ofNullable(serverZone).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }

    @Override
    public String getGrayFromServer(ServiceInstance server) {
        String serverGray = getMetadataFromServer(server).get(LinkFlowConstant.GRAY);
        return Optional.ofNullable(serverGray).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.FALSE_STR);
    }

    @Override
    public String getDefaultGroupKey() {
        return GROUP_KEY;
    }

    @Override
    public String getGroup() {
        String group = getGroup(getDefaultGroupKey());
        return Optional.ofNullable(group).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }

    @Override
    public String getServiceId() {
        return registration.getServiceId().toLowerCase();
    }

    @Override
    public void setRule(ConfigRule configRule) {
        configMemory.putConfig(LinkFlowConstant.RULE, configRule);
    }

    /**
     * 获取分区
     * @param server 服务
     * @return
     */
    @Override
    public String getGroupFromServer(ServiceInstance server) {
        //先获取分组的key
        String serverGroupKey = getGroupKeyFromServer(server);
        //从元数据中获取分组
        String serverGroup = getMetadataFromServer(server).get(serverGroupKey);
        return Optional.ofNullable(serverGroup).filter(StringUtils::isNotEmpty).orElse(LinkFlowConstant.DEFAULT);
    }

    @Override
    public Map<String,String> getInfo() {
        String group = getGroup();
        String zone = getZone();
        String version = getVersion();
        Map<String,String> map = new HashMap<>(8);
        map.put("group",group);
        map.put("zone",zone);
        map.put("version",version);
        return map;
    }

}
